
#include <grouptreeitemview.h>
#include <QLabel>
#include <QPainter>
#include <QFontMetrics>
#include <stdio.h>

static QColor hiColor ()
{
  QColor color (0xe0, 0xe0, 0xe0);
  return color;
}

static QColor loColor ()
{
  return QPalette().color(QPalette::Active, QPalette::Window);
}


static QBrush hiBrush()
{
  QBrush brush (hiColor());
  return brush;
}

static QBrush loBrush ()
{
  static QBrush brush (loColor());
  return brush;
}

QColor color_interpolate (const QColor& colA, const QColor& colB, float f)
{
  int r = (float)colA.red()   * (1.0f - f) + (float)colB.red()    * f;
  int g = (float)colA.green() * (1.0f - f) + (float)colB.green()  * f;
  int b = (float)colA.blue()  * (1.0f - f) + (float)colB.blue()   * f;
  int a = (float)colA.alpha() * (1.0f - f) + (float)colB.alpha()  * f;

  return QColor (r, g, b, a);
}


class CatNameLabel : public QLabel
{
private:
public:
  CatNameLabel (const QString& name, QWidget* parent)
    : QLabel (name, parent)
  {
    QFontMetrics fm (font());

    setAlignment(Qt::AlignCenter);
    setMinimumHeight(fm.height() + 6);
    setMinimumWidth(fm.width(name) + 6);
    setMaximumHeight(minimumHeight());
    setMaximumWidth(minimumWidth());

  }

  virtual void	paintEvent (QPaintEvent * event)
  {
    QPainter p (this);

    p.fillRect(0, 0, width(), height(), hiColor());

    QLabel::paintEvent (event);
  }
};

class CatSepLabel : public QLabel
{
private:
  bool  _leftHi;
  bool  _rightHi;
public:
  CatSepLabel (QWidget* parent, bool leftHi, bool rightHi)
    : QLabel (parent)
    , _leftHi (leftHi)
    , _rightHi (rightHi)
  {
    QFontMetrics fm (font());
    setMinimumHeight(fm.height() + 6);
    setMinimumWidth((fm.height() + 6) / 2 + 2);
    setMaximumHeight(minimumHeight());
    setMaximumWidth(minimumWidth());
  }

  virtual void	paintEvent (QPaintEvent * event)
  {
    QPainter p (this);
    p.setRenderHint(QPainter::Antialiasing);

    const QPoint points [3] = {
      QPoint (0, 0),
      QPoint (width()-2, height() / 2),
      QPoint (0, height())
    };

    if (_rightHi)
      {
        p.fillRect(0, 0, width(), height(), hiColor());
      }
    else
      {
        p.fillRect(0, 0, width(), height(), loColor());
      }

    if (_leftHi)
      {
        p.setPen(hiColor());
        p.setBrush(hiBrush());
      }
    else
      {
        p.setPen(loColor());
        p.setBrush(loBrush());
      }
    p.drawPolygon(points, 3);


    const QPoint linePoints_a[4] = {
      QPoint (0, 0),
      QPoint (width()-3, height() / 2-1),
      QPoint (width()-3, height() / 2+1),
      QPoint (0, height()),
    };



    p.setTransform(QTransform::fromTranslate(1, 0));
    QColor col = color_interpolate(hiColor(), loColor(), 0.5);
    p.setPen(col);
    p.drawLines(linePoints_a, 2);

    p.setTransform(QTransform::fromTranslate(0, 0));
    col = color_interpolate(hiColor(), loColor(), 0.66);
    p.setPen(QColor (0xff, 0xff, 0xff, 0xff));
    p.drawLines(linePoints_a, 2);


    QLabel::paintEvent (event);
  }

};

class CatFillLabel : public QLabel
{
public:
  CatFillLabel (QWidget* parent)
    : QLabel (parent)
  {
    QFontMetrics fm (font());
    setMinimumHeight(fm.height() + 6);
    setMaximumHeight(minimumHeight());
  }

  virtual void	paintEvent (QPaintEvent * event)
  {
    QPainter p (this);

    p.fillRect(0, 0, width(), height(), loColor());

    QLabel::paintEvent (event);
  }
};

GroupTreeItemView::GroupTreeItemView(QWidget *parent)
  : QFrame(parent)
  , _item (0)
  , _layout(0)
{
}


void GroupTreeItemView::SetGroupTreeItem(Category *item)
{
  if (_item)
    {
      disconnect(_item, SIGNAL(Changed()), this, SLOT(UpdateGUI()));
    }
  _item = item;
  connect (_item, SIGNAL(Changed()), this, SLOT(UpdateGUI()));
  UpdateGUI ();
}

void GroupTreeItemView::UpdateGUI ()
{
  if (!_layout)
    {
      _layout = new QGridLayout(this);
      _layout->setSpacing(0);
      _layout->setContentsMargins(QMargins (0, 0, 0, 0));
    }
  QLayoutItem* child = 0;
  while ((child = _layout->takeAt(0)) != 0)
    {
      QWidget* wgt = child->widget();
      if (wgt)
        {
          _layout->removeWidget(wgt);
          wgt->deleteLater();
        }

      delete child;
    }
  if (!_item)
    {
      return;
    }


  int j=0;
  int l = _item->GetNumberOfStages() - 1;
  for (int i=0; i<=l; i++)
    {
      _layout->addWidget(new CatNameLabel(_item->GetStageName(i), this), 0, j++);
      _layout->addWidget(new CatSepLabel(this, true, i != l), 0, j++);
    }
  _layout->addWidget(new CatFillLabel (this), 0, j++);
  fflush(stdout);
}
